<?php

namespace app\admin\service;

use app\admin\dao\MarginDao;
use app\admin\model\UM as UM;
use app\common\job\MarginAutoConfirm;
use app\common\model\Consulting;
use app\common\model\Margin;
use app\common\model\Member;
use app\common\model\Trade;
use app\common\service\SubscriptionMessageService;
use think\exception\ValidateException;
use think\facade\Queue;

/**
 * Class MarginService
 * @package app\admin\service
 */
class MarginService
{
    // dao
    private MarginDao $marginDao;

    public function __construct(MarginDao $marginDao)
    {
        $this->marginDao = $marginDao;
    }

    /**
     * 列表
     * @param UM|null $user
     * @param int|null $page
     * @param int|null $page_size
     * @return array
     */
    public function index(?UM $user, $memberId, ?int $page = null, ?int $page_size = null): array
    {
        return $this->marginDao->list($memberId, $page, $page_size);
    }

    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return  Margin
     */
    public function add(UM $user, array $params): Margin
    {
        $tradeId = $params['tradeId'] ?? null;
        $total = $params['total'] ?? null;
        $describe = $params['describe'] ?? null;

        if (!is_id($tradeId)) {
            throw new ValidateException('请选择一个订单');
        }

        if (!is_numeric($total)) {
            throw new ValidateException('请输入正确的金额');
        }
        $total = round($total, 2);
        if (bccomp($total, 0, 2) <= 0) {
            throw new ValidateException('罚款金额应该大于0');
        }

        if (is_blank($describe)) {
            throw new ValidateException('请输入备注');
        }


        $trade = Trade::where('id', $tradeId)->find();
        if (!$trade) {
            throw new ValidateException('该订单不存在或已被删除');
        }

        $orderNo = $trade->orderNo;
        $consultingId = $trade->consultingId;

        if (!is_id($consultingId)) {
            throw new ValidateException('该订单不属于咨询订单');
        }
        $consulting = Consulting::where('id', $consultingId)->find();
        if (!$consulting) {
            throw new ValidateException('该订单不属于咨询订单。');
        }

        $lawyerId = $consulting->lawyerId;

        if (!is_id($lawyerId)) {
            throw new ValidateException('该订单所属咨询暂未分配律师');
        }
        $lawyer = Member::where('id', $lawyerId)->find();
        if (!$lawyer) {
            throw new ValidateException('该律师不存在或已被删除');
        }
        if ($lawyer->svip != 1) {
            throw new ValidateException('该律师不是SVIP，不能扣除保证金');
        }

        $data = [
            'userId' => $user->id,
            'tradeId' => $tradeId,
            'memberId' => $lawyerId,
            'total' => $total,
            'describe' => $describe,

            'confirmStatus' => 0,
            'confirmMemberId' => 0,
            'confirmTime' => 0,
            'status' => 0,
        ];

        $record = $this->marginDao->insertActive($data, []);

        // 添加自动确认延时任务
        $job_data = ['id', $record->id];
        Queue::later(24 * 3600, MarginAutoConfirm::class, $job_data);

        // 公众号消息
        $this->getSubscriptionMessageService()->messageMargin($lawyerId, $describe, $total, $orderNo, time());

        return $record;
    }

    private function getSubscriptionMessageService(): SubscriptionMessageService
    {
        return app(SubscriptionMessageService::class);
    }

}
